package com.me.study.javaBasic.exception;

/**
 * 本类用来演示Java异常的案例 作者 JSD1402班 时间 下午2:02:59
 */
public class ExceptionDemo {

	public static void main(String[] args) {
		/*
		 * Integer i = 6; Integer j = 0; Integer result = divide(i, j); System.out.println(result);
		 */

		Double d1 = 9.0;
		Double d2 = null;
		// Double result2 = divide(d1, d2);
		try {
			Double result = divide(d1, d2);
			System.out.println(result);
			return;
		} catch (DivideNotNullException e) {
			// // System.err.println("除数或者被除数不能为空!");
			// // e.printStackTrace();
			// // 除数或者被除数不能为空!
			// // com.me.javaCore.exception.DivideNotNullException
			// // at com.me.javaCore.exception.ExceptionDemo.divide(ExceptionDemo.java:57)
			// // at com.me.javaCore.exception.ExceptionDemo.main(ExceptionDemo.java:16)
			//
			// throw e;
			// // 除数或者被除数不能为空!
			// // Exception in thread "main" com.me.javaCore.exception.DivideNotNullException
			// // at com.me.javaCore.exception.ExceptionDemo.divide(ExceptionDemo.java:52)
			// // at com.me.javaCore.exception.ExceptionDemo.main(ExceptionDemo.java:16)
			//
			throw new RuntimeException("除数或者被除数不能为空!", e);
		} catch (DivideNotZeroException e) {
			System.err.println("除数不能为0!");
		} finally {
			System.out.println("I'm finally block!");
		}
	}

	/**
	 * 此方法计算讲个数字的商
	 */
	public static Integer divide(Integer i, Integer j) {
		/*
		 * //非空判断 if(i == null || j == null){ return null; }
		 */
		try {
			return i / j;
		} catch (NullPointerException e) {
			// System.out.println("除数或者被除数不能为空!");
			throw new NullPointerException("除数或者被除数不能为空!");
		} catch (ArithmeticException e) {
			throw new ArithmeticException("除数不能为0!");
		}
	}

	/**
	 * 此方法计算讲个浮点数的商
	 * 
	 * @throws DivideNotZeroException
	 */
	public static Double divide(Double d1, Double d2)
			throws DivideNotNullException, DivideNotZeroException {
		if (d1 == null || d2 == null) {
			throw new DivideNotNullException();
		}
		if (d2 == 0) {
			throw new DivideNotZeroException();
		}
		return d1 / d2;
	}
}
